package com.coocaa.ops.api.common.util;

import com.coocaa.ops.api.common.model.HeaderModel;
import com.coocaa.ops.common.core.base.enums.TvModelEnums;
import com.coocaa.ops.common.tool.json.JsonUtil;
import com.coocaa.ops.common.tool.string.StringUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

/**
 * @author tangminghong
 * @date 2020-02-10 17:41
 */
@Slf4j
public class RequestUtil {

    /**
     * 解析客户端请求头参数
     *
     * @param request
     * @return
     */
    public static HeaderModel getHeader(HttpServletRequest request) {
        HeaderModel header = new HeaderModel();
        try {
            // 公共参数
            header.setAcceptEncoding(StringUtil.avoidNull(request.getHeader("Accept-Encoding")));
            header.setCUDID(StringUtil.avoidNull(request.getHeader("cUDID")));
            String mac = StringUtil.replaceBlank(request.getHeader("MAC"), "000000000000").replaceAll(" ", "");
            if ("0".equals(mac)) {
                mac = "000000000000";
            }
            header.setMac(mac);
            header.setCModel(StringUtil.avoidNull(request.getHeader("cModel")));
            header.setCChip(StringUtil.avoidNull(request.getHeader("cChip")));
            header.setCSize(StringUtil.replaceBlank(request.getHeader("cSize"), "0"));
            header.setCResolution(StringUtil.avoidNull(request.getHeader("cResolution")));
            header.setCTcVersion(StringUtil.replaceBlank(request.getHeader("cTcVersion"), "0"));
            header.setCFMode(StringUtil.replaceBlank(request.getHeader("cFMode"), TvModelEnums.Default.toString()));

            header.setCUserInfo(StringUtil.avoidNull(request.getHeader("cUserInfo")));
            // 教育业务公共参数
            header.setVAcceptSources(StringUtil.avoidNull(request.getHeader("vAcceptSources")));
            header.setVAppVersion(StringUtil.replaceBlank(request.getHeader("cAppVersion"), "0000000"));
            //DMP-用户状态需要的id
            header.setCOpenId(StringUtil.avoidNull(request.getHeader("cOpenId")));
            header.setVUserId(StringUtil.avoidNull(request.getHeader("vuserid")));
            header.setThirdUserId(StringUtil.avoidNull(request.getHeader("external_id")));

            header.setVAppID(request.getHeader("vAppID"));
            header.setCPattern(StringUtil.replaceBlank(request.getHeader("cPattern"), TvModelEnums.normal.toString()));
            //江苏编码
            header.setCAreaCode(StringUtil.avoidNull(request.getHeader("cAreaCode")));
            //根据IP获取的区域编码
            header.setCIPRegionCode(StringUtil.avoidNull(request.getHeader("cIPRegionCode")));
            //品牌
            header.setCBrand(StringUtil.avoidNull(request.getHeader("cBrand")));
            //支持资源
            header.setSupportSource(StringUtil.avoidNull(request.getHeader("supportSource")));
            //电视所属的源
            header.setSource(StringUtil.avoidNull(request.getHeader("source")));

            } catch (Exception e) {
            header = getDefaultHeader();
            log.error("从http请求中获取头部信息出错，将返回默认设请求头信息！原因：" + e.getMessage());
            log.error("默认请求头信息：" + header.toString());
        }
        log.debug("\nHTTP请求头信息：\n\t" + JsonUtil.obj2Json(header));
        return header;
    }


    /**
     * 获取策略对应的请求头信息
     *
     * @return
     */
    private static HeaderModel getDefaultHeader() {
        HeaderModel header = new HeaderModel();
        // 公共参数
        header.setAcceptEncoding("");
        header.setCUDID("");
        header.setMac("000000000000");
        header.setCModel("");
        header.setCChip("");
        header.setCSize("0");
        header.setCResolution("");
        header.setCTcVersion("0");
        header.setCFMode("Default");
        //header.setCPattern(TvModelEnums.normal.toString());
        // 教育业务公共参数
        header.setVAcceptSources("sky,voole,tencent,iqiyi");
        header.setVAppID("SkyMoviePlatform");
        header.setVAppVersion("0");
        return header;
    }
}
